package zrweb;

import java.io.DataOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DriverManager; 


import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Shot
{
	private Connection connect = null; 
	private String message = "";
	private PreparedStatement ps = null;
	
	public void getShot(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		// get the parameters 
	    String username = request.getParameter("username"); 
	    String longitude = request.getParameter("lon"); 
	    String latitude  = request.getParameter("lat"); 
	    String velocity = request.getParameter("velocity");
	    String z_acc_ = request.getParameter("z_acc");
	    String azimuth_ = request.getParameter("azimuth");
	    String roll_ = request.getParameter("roll");
	    System.out.println("got parameters!"); 
	    //transform all values to usable ones in the equations.    		
	    double lon = Double.parseDouble(longitude); 
	    double lat = Double.parseDouble(latitude); 
	    double v = (Double.parseDouble(velocity)*75); //maximum velocity = 75 m/s 
	    double z_acc = Double.parseDouble(z_acc_); 
	    double roll__ = Double.parseDouble(roll_); 
	    double angle = 0;
	    if (z_acc < 0) { 
	    	angle = Math.toRadians(Math.abs(roll__- 90)); 
	    } else { 
	    	angle = 0;
	    }
	    /*double azi = ((Double.parseDouble(azimuth_)+90) % 360); 
	    
	    int g = 1; //gravity constant (10 times lower to get 'slow' shots)
	
	    double dist = (v*v*Math.sin(2*angle))/g;
	    
	    int lat_sign = 0;
	    int lon_sign = 0;
	    
	    if (0.0 <= azi && azi <= 90.0) {
	    	 lat_sign = 1;
	    	 lon_sign = 1;
	    } else if (90.0<azi && azi<=180.0){
	    	 lat_sign = -1;
	    	 lon_sign = 1;
	    } else if (180.0<azi && azi<=270.0){
	    	 lat_sign = -1;
	    	 lon_sign = -1;
	    } else if (270.0<azi && azi<=360.0){
	    	 lat_sign = 1;
	    	 lon_sign = -1;
	    }
	    
	    double impact_lat = Math.cos(azi)*dist*lat_sign/111000 + lat;
	    double impact_lon = Math.sin(azi)*dist*lon_sign/111000 + lon;*/
	    
	    // connect to database to update online column and clear locations 
	    ConnDB conndb = new ConnDB(); 
	    // NOTE: string -> varchar for posrgresql query by using ''
	    
		String selectSQL = "select username from userinfor where onservice = TRUE and" + 
						   " username !='" + username + "'";
		// give feedback on whether can select or not for Android mobile
		DataOutputStream dos = new DataOutputStream(response.getOutputStream());
		try
		{
			connect = conndb.getConn();
			Statement stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
			ResultSet rs = stmt.executeQuery(selectSQL);
			// at the moment, it is the simplest criterion that client who is listed in the first row of the query result
			// be labeled as dead.
			if(rs.first()) //if there are at least one row in the query result
			{
				message = rs.getString("username");
				String updateSQL = "update userinfor set onservice = FALSE where username='" + rs.getString("username") + "'";
				try 
				{
					connect = conndb.getConn();
					ps = connect.prepareStatement(updateSQL);
					ps.executeUpdate();
				} 
				catch (SQLException e) 
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// pass the query result to the mobile side
				dos.writeUTF("Succeed " + message);
				rs.close();  
		        stmt.close();
			}
			else
			{
				dos.writeUTF("Succeed ");
				rs.close();  
		        stmt.close();
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			ConnDB.releaseConnection(connect);
		} 
	     
	    /*String SQL1 = 	"SELECT u.usrname INTO cmdb.hitlist" 						+
			   			"FROM cmdb.usrlist u"										+
			   			"WHERE SQRT(POW(("+ impact_lat +" - u.current_lat),2) + "	+
			   			"POW(("+impact_lon+" - u.current_lon),2))*111000 <300.0"	+
			   			"AND u.online = TRUE"										+
			   			"AND (SQRT(POW((u.safe_lat - u.current_lat),2) + "			+
			   			"POW((u.safe_lon - u.current_lon),2))*111000)< 50 "			+
			   			"AND NOT u.usrname = '"+username+"'";
	    
	    String SQL2 =	"UPDATE cmdb.usrlist"										+
	    				"SET rank = rank + (SELECT COUNT(usrname) FROM cmdb.hitlist)" +
	    				"WHERE usrname='user1'";
	    
	    String SQL3 =	"UPDATE cmdb.usrlist"										+
	    				"SET rank = rank-1"											+
	    				"WHERE cmdb.usrlist.usrname=cmdb.hitlist.usrname";
	    				
	    String SQL4 =	"DROP TABLE cmdb.hitlist";
		    
	    try { 
	    	Statement stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
	        stmt.executeUpdate(SQL1);
	        System.out.println("SQL1 ok");
	        } catch (SQLException e) {e.printStackTrace();}
	    try { 
	    	Statement stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
	        stmt.executeUpdate(SQL2);
	        System.out.println("SQL2 ok");
	        } catch (SQLException e) {e.printStackTrace();}
	    try { 
	    	Statement stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
	        stmt.executeUpdate(SQL3);
	        System.out.println("SQL3 ok");
	        } catch (SQLException e) {e.printStackTrace();}
	    try { 
	    	Statement stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
	        stmt.executeUpdate(SQL4);
	        System.out.println("SQL4 ok");
	        } catch (SQLException e) {e.printStackTrace();}
	    finally 
		    { 
		      ConnDB.releaseConnection(connect); 
		    } */
	}
}	 

